/*
 * CustomerFacadeBean.java
 *
 * Created on 8 May 2007, 01:23
 * Copyright (C) 2007   Yuezhong Zhang
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package usyd.comp5348;

import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Yuezhong Zhang
 */
@Stateless(mappedName="ejb/Customer")
public class CustomerFacadeBean implements CustomerFacadeRemote, CustomerFacadeLocal {
    
    private Logger logger = Logger.getLogger("usyd.comp5348.CustomerFacadeBean");
    @PersistenceContext
    private EntityManager em;
    
    /** Creates a new instance of CustomerFacadeBean */
    public CustomerFacadeBean() {
    }
    /**
     * Create a customer with customer info
     * @param  cust   a CustomerInfo object, cannot be null
     * @return  id    return the id of newly created customer
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public int createCustomer(Customer cust) {
        em.persist(cust);
        em.flush();
        return cust.getId();
    }
    /**
     * Modifying exisiting customer with new customer information
     * @param  cust   new information about customer, cannot be null
     * @throw  CustomerException  if couldn't find the customer to modify
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void modifyCustomer(Customer cust) throws CustomerException {
        Customer c = em.find(Customer.class, cust.getId());
        if(c == null) {
            CustomerException ext = new CustomerException("Couldn't find customer with id="+ cust.getId());
            logger.warning(ext.getMessage());
            throw ext;
        }else {
            c.setName(cust.getName());
            c.setAddress(cust.getAddress());
            c.setCardName(cust.getCardName());
            c.setCardNumber(cust.getCardNumber());
            c.setCardExpireDate(cust.getCardExpireDate());
            em.merge(c);
            em.flush();
        }
    }
    /**
     * Delete a customer
     *@param delete customer with customer info (id must be provided")
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void deleteCustomer(Customer cust) {
        Customer c = em.find(Customer.class, cust.getId());
        if(c !=null){
            em.remove(c);
            em.flush();
        }
    }
    /**
     *Get the customer's information by its id
     *@param  id customer's id
     *@return CustomerInfo  information about queried customer
     */
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public Customer getCustomerById(int id) {
        return em.find(Customer.class, id);
        
    }
    
}
